Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
## Chapter 14 File Management Visual Basic provides all the capabilities you need for managing files and folders on your disks. The term file management refers to everything you do with files and folders other than actually writing to and reading from them. Deleting files, creating folders, copying files from one drive to another, and looking up the amount of free space on a drive are all examples of file management. Not every program will need these capabilities, but it is comforting to know that Visual Basic has a full set of tools for file management when and if you need them. Visual Basic’s file management tools can be divided into three categories. First, we have what I call the traditional techniques, using Basic statements that have been part of the language from the beginning. Next, and new with the current version of Visual Basic, are the object-oriented file management techniques. For the most part, the traditional and object-oriented techniques have the same capabilities, differing only in the syntax used. Finally, Visual Basic has several controls designed specifically for file management tasks. TIP: Which Should You Use? For most file management tasks, you can use either the traditional or the object-oriented techniques. In fact, you can use both in the same program. All things being equal, however, I suggest you use the object-oriented techniques whenever possible. Given that programming in general is moving in the object-oriented direction, it seems advisable to “go with the flow,” so to speak. ### File Management—The Traditional Way This section demonstrates how to use Visual Basic’s traditional file management statements. #### Deleting Files To delete a file, use the Kill statement
    Kill filename
where filename is a string expression specifying the file or files to delete. It can contain a drive and path name, as well as the so-called wildcard characters * and ?. Deleting a file that does not exist, a file that’s open, or a file for which you do not have the needed access privileges will generate an error. As you may know, you can use the wildcard characters to specify groups of files. The * character stands for any sequence of zero or more characters, while ? stands for any single character. Table 14.1 shows some examples. #### Deleting And Creating Folders To delete a folder (also called a subdirectory), use the RmDir statement
    RmDir path
Table 14.1 Wildcard characters.
File Specification | Matches | Does Not Match
--- | --- | ---
DATA*.* | DATA.DAT | MYDATA.DAT
| DATASEPT.DAT | DATES.TXT
| DATASUMMARY.TXT |
DATA?.A* | DATA1.ASC | DATA.ASC
| DATA2.A | DATASEPT.ASC
| DATAX.ARG | DATAX.DAT
*.* | All files | -
where path is a string expression that specifies the folder to delete and can include a drive specification. If you try to delete a folder that does not exist or is not empty (contains one or more files or folders), an error is generated. To create a new folder, use the MkDir statement
    MkDir path
where path specifies the name of the folder to create and can include a full path and drive specification. If only a name is specified, the new folder is created in the current folder on the current drive. #### Changing To Another Folder Or Drive To make another folder current, use the ChDir statement
    ChDir path
where path specifies the new folder and can include a drive specification. If no drive is specified, ChDir changes the current folder on the current drive. ChDir does not change the current drive. If the current drive is C: and you execute
    ChDir “D:\DATA”
the current directory on drive D: is changed to \Data. However, C: remains the current drive and the current directory on drive C: remains unchanged. To make another drive current, use the ChDrive statement
    ChDrive drive
where drive specifies the new drive to make current. Only the first character of drive is significant. Attempting to make an invalid drive current will generate an error. #### Getting Path And Folder Information To determine the current path on a specified drive, use the CurDir function
    [path =] CurDir[(drive)]
where drive specifies the drive of interest. Only the first character of drive is significant; unless it refers to a valid drive, an error will be generated. If the drive argument is omitted, CurDir returns the current path on the current drive. You can obtain the path of the current Visual Basic application by reading the App object’s Path property:
    CurrentPath = App.Path
This property returns the path where the project file (VBP) is located when the application is running in the Visual Basic environment, and the path where the executable file (EXE) is located when running the program as an executable file. This property is extremely useful when you want to be sure that a program’s data or configuration files are stored in the same folder as the EXE file. Be aware that the value returned by this property does not include a trailing backslash, so you must add it when creating a fully qualified file name. The following code opens a file named CONFIG.DAT in the program’s EXE directory:
    Filename = App.Path
    Filename = Filename & “\CONFIG.DAT”
    Open Filename For Input as #1
> TIP: Current Path Vs. Application’s Path What’s the difference between the current path on a drive and an application’s path? An application’s path is simply the path to the folder containing the program’s executable file. It has no special status, except that each application may want to keep certain files in this folder. A drive’s current path points to the current default folder on that drive. Each drive in our system can have only one default path at a time, and this information is maintained by the operating system (although it can be changed by programs). The default path is the location where file operations will take place if a particular path is not specified. For example, the statement
>     Open "C:DATA.TXT" For Output As #1
> will open a file in the folder pointed to by drive C:’s current path. A drive letter followed by only a colon always refers to the drive’s current path. The operating system also keeps track of the default drive, which is the disk where file operations will occur if a specific disk is not requested. Thus
>     Open "DATA.TXT" For Output As #1
> would open the file on the default drive, in the folder indicated by that drive’s default path. Likewise
>     Open "\SALES\DATA.TXT" For Output As #1
would open the file in the \SALES folder on the default drive.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.